package 实验六;

interface Animal {
    void makeSound(); // 每个动物都应该能够发出声音
}

class Dog implements Animal {
    @Override
    public void makeSound() {
        System.out.println("汪汪!");
    }
}

class Cat implements Animal {
    @Override
    public void makeSound() {
        System.out.println("喵喵!");
    }
}

class Cow implements Animal {
    @Override
    public void makeSound() {
        System.out.println("哞哞!");
    }
}

class AnimalFactory {
    public static Animal getAnimal(String animalType) {
        if (animalType == null) {
            return null;
        }
        if (animalType.equalsIgnoreCase("DOG")) {
            return new Dog();
        } else if (animalType.equalsIgnoreCase("CAT")) {
            return new Cat();
        } else if (animalType.equalsIgnoreCase("COW")) {
            return new Cow();
        }

        return null; // 如果没有匹配的类型，则返回 null
    }
}

public class 工厂模式 {
    public static void main(String[] args) {
        Animal dog = AnimalFactory.getAnimal("DOG");
        Animal cat = AnimalFactory.getAnimal("CAT");
        Animal cow = AnimalFactory.getAnimal("COW");

        dog.makeSound();
        cat.makeSound();
        cow.makeSound();
    }
}
